/* $Id: bsd.c,v 1.1 2005/06/20 16:34:28 jfb Exp $ */
/*
 * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/socket.h>

#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>

#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ifaddrs.h>

#include "if.h"
#include "dlink.h"
#include "pforge.h"

#define PFIF_BSD_FLMASK		0xffff


/*
 * pf_netif_probe()
 *
 * BSD version of the probe function, using the deceptively simple
 * getifaddrs() interface.
 */
int
pf_netif_probe(void)
{
	int nbif, sock;
	size_t len;
	PF_NETIF *nif;
	struct ifreq ifr;
	struct if_data *ifdatp;
	struct ifaddrs *ifa, *ifp;
	struct ifmediareq ifm;
	struct sockaddr_dl *sdl;

	nbif = 0;
	if (getifaddrs(&ifa) == -1) {
		return (-1);
	}

	for (ifp = ifa; ifp != NULL; ifp = ifp->ifa_next) {
		/* we are only interested in link-level entries */
		if ((ifp->ifa_addr == NULL) ||
		    (ifp->ifa_addr->sa_family != AF_LINK))
			continue;

		sdl = (struct sockaddr_dl *)ifp->ifa_addr;
		ifdatp = (struct if_data *)ifp->ifa_data;

		if ((nif = pf_netif_alloc()) == NULL)
			return (-1);

		strlcpy(nif->if_name, ifp->ifa_name, sizeof(nif->if_name));
		nif->if_mtu = ifdatp->ifi_mtu;
		nif->if_flags = ifp->ifa_flags & PFIF_BSD_FLMASK;
		nif->if_index = sdl->sdl_index;

		nif->if_hwalen = sdl->sdl_alen;
		if (nif->if_hwalen > 0)
			memcpy(nif->if_hwaddr, LLADDR(sdl),
			    sizeof(nif->if_hwaddr));

		switch (sdl->sdl_type) {
		case IFT_LOOP:
			nif->if_type = PFIF_TYPE_LOOP;
			break;
		case IFT_ETHER:
			nif->if_type = PFIF_TYPE_ETHER;
			break;
		case IFT_FASTETHER:
			nif->if_type = PFIF_TYPE_FASTETHER;
			break;
		case IFT_ISO88025:
			nif->if_type = PFIF_TYPE_TKNRING;
			break;
		case IFT_FDDI:
			nif->if_type = PFIF_TYPE_FDDI;
			break;
		case IFT_IEEE80211:
			nif->if_type = PFIF_TYPE_IEEE80211;
			break;
		case IFT_PPP:
			nif->if_type = PFIF_TYPE_PPP;
			break;
		case IFT_MODEM:
			nif->if_type = PFIF_TYPE_MODEM;
			break;
		case IFT_USB:
			nif->if_type = PFIF_TYPE_USB;
			break;
		case IFT_ISDN:
			nif->if_type = PFIF_TYPE_ISDN;
			break;
		default:
			nif->if_type = PFIF_TYPE_OTHER;
			break;
		}

		if (pf_netif_add(nif, nif->if_index) < 0) {
			pf_netif_free(nif);
			return (-1);
		}
		nbif++;
	}

	freeifaddrs(ifa);

	return (nbif);
}
